#include "title/TitleBar.h"

#include <qchar.h>
#include <qevent.h>
#include <qnamespace.h>
#include <qobject.h>
#include <qobjectdefs.h>
#include <qpixmap.h>
#include <qpoint.h>
#include <qpushbutton.h>
#include <qsize.h>
#include <qsvgrenderer.h>
#include <qwidget.h>
#include <QSvgRenderer>
#include <QPixmap>
#include <QIcon>
#include <QPainter>

#define BUTTON_HEIGHT 20 // 按钮高度;
#define BUTTON_WIDTH 20  // 按钮宽度;
#define TITLE_HEIGHT 20  // 标题栏高度;

TitleBar::TitleBar(QWidget *parent) : QWidget(parent)
{
    initUI();
    initBindingSignalSlot();
    initIcon();
    loadStyleSheet();

    setTitleIcon(QString(":/resources/icon/title_icon.svg"));
}

TitleBar::~TitleBar() {}

void TitleBar::initUI()
{
    mainLayout = new QHBoxLayout();
    mainLayout->setContentsMargins(0, 0, 0, 0);

    leftLayout = new QHBoxLayout;
    leftLayout->setContentsMargins(2, 3, 0, 0);
    menubar = new QMenuBar();
    fileMenu = new QMenu(tr("文件"));
    editMenu = new QMenu(tr("编辑"));
    toolMenu = new QMenu(tr("工具"));
    viewMenu = new QMenu(tr("视图"));

    // 为每个菜单添加示例动作
    fileMenu->addAction(tr("新建")); // 添加文件菜单的子项
    editMenu->addAction(tr("撤销")); // 添加编辑菜单的子项
    toolMenu->addAction(tr("选项")); // 工具菜单已有子项
    viewMenu->addAction(tr("全屏")); // 视图菜单已有子项

    menubar->addMenu(fileMenu);
    menubar->addMenu(editMenu);
    menubar->addMenu(toolMenu);
    menubar->addMenu(viewMenu);
    leftLayout->addSpacing(10);
    leftLayout->addWidget(menubar);
    leftLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
    // leftLayout->addStretch();

    centerLayout = new QHBoxLayout;
    centerLayout->setContentsMargins(0, 0, 0, 0);
    titleIcon = new QLabel;
    title = new QLabel(tr("开发助手"));
    textLabel = new QLabel;
    textLabel->setObjectName("textInfo");
    centerLayout->addStrut(1);
    centerLayout->addWidget(titleIcon);
    centerLayout->addWidget(title);
    centerLayout->addStrut(1);
    centerLayout->addWidget(textLabel);
    // centerLayout->addStrut(1);

    rightLayout = new QHBoxLayout;
    rightLayout->setContentsMargins(0, 0, 0, 0);
    minButton = new QPushButton;
    restorButton = new QPushButton;
    maxButton = new QPushButton;
    closeButton = new QPushButton;

    minButton->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
    restorButton->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
    maxButton->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
    closeButton->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));

    minButton->setObjectName("minButton");
    restorButton->setObjectName("restorButton");
    maxButton->setObjectName("maxButton");
    closeButton->setObjectName("closeButton");
    restorButton->setVisible(false);

    rightLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
    rightLayout->addWidget(minButton);
    rightLayout->addWidget(restorButton);
    rightLayout->addWidget(maxButton);
    rightLayout->addWidget(closeButton);

    mainLayout->addLayout(leftLayout);
    mainLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
    mainLayout->addLayout(centerLayout);
    mainLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
    mainLayout->addLayout(rightLayout);

    setLayout(mainLayout);
}

void TitleBar::initIcon()
{
    int width = 12;
    int height = 12;
    QSvgRenderer minRenderer(QString(":/resources/icon/min_window.svg"));
    QPixmap pixmap(width, height);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    minRenderer.render(&painter);
    QIcon icon(pixmap);
    minButton->setIcon(icon);
    minButton->setIconSize(pixmap.size());

    QSvgRenderer restoreRenderer(QString(":/resources/icon/restore_window.svg"));
    QPixmap pixmap1(width, height);
    pixmap1.fill(Qt::transparent);
    QPainter painter1(&pixmap1);
    restoreRenderer.render(&painter1);
    QIcon icon1(pixmap1);
    restorButton->setIcon(icon1);
    restorButton->setIconSize(pixmap1.size());

    QSvgRenderer maxRenderer(QString(":/resources/icon/max_window.svg"));
    QPixmap pixmap2(width, height);
    pixmap2.fill(Qt::transparent);
    QPainter painter2(&pixmap2);
    maxRenderer.render(&painter2);
    QIcon icon2(pixmap2);
    maxButton->setIcon(icon2);
    maxButton->setIconSize(pixmap2.size());

    QSvgRenderer closeRenderer(QString(":/resources/icon/close_window.svg"));
    QPixmap pixmap3(width, height);
    pixmap3.fill(Qt::transparent);
    QPainter painter3(&pixmap3);
    closeRenderer.render(&painter3);
    QIcon icon3(pixmap3);
    closeButton->setIcon(icon3);
    closeButton->setIconSize(pixmap3.size());

    this->repaint();
}

void TitleBar::loadStyleSheet()
{
    QFile file(QString(":/resources/style/titlebar.qss"));
    file.open(QFile::ReadOnly);
    if (file.isOpen())
    {
        QString styleSheet = this->styleSheet();
        styleSheet += file.readAll();
        this->setStyleSheet(styleSheet);
    }
}

void TitleBar::initBindingSignalSlot()
{
    connect(minButton, &QPushButton::clicked, this, &TitleBar::onMinButtonClicked);
    connect(restorButton, &QPushButton::clicked, this, &TitleBar::onRestorButtonClicked);
    connect(maxButton, &QPushButton::clicked, this, &TitleBar::onMaxButtonClicked);
    connect(closeButton, &QPushButton::clicked, this, &TitleBar::onCloseButtonClicked);
}

// 按钮操作槽函数
void TitleBar::onMinButtonClicked()
{
    emit signalButtonMinClicked();
}

void TitleBar::onRestorButtonClicked()
{
    maxButton->setVisible(true);
    restorButton->setVisible(false);
    emit signalButtonRestoreClicked();
}

void TitleBar::onMaxButtonClicked()
{
    maxButton->setVisible(false);
    restorButton->setVisible(true);
    emit signalButtonMaxClicked();
}

void TitleBar::onCloseButtonClicked()
{
    emit signalButtonCloseClicked();
}

void TitleBar::saveRestoreInfo(const QPoint point, const QSize size)
{
    restoreSize = size;
    restorePos = point;
}

void TitleBar::getRestoreInfo(QPoint &point, QSize &size)
{
    point = restorePos;
    size = restoreSize;
}

// 事件处理     ======================================================
void TitleBar::paintEvent(QPaintEvent *event)
{
    // 设置背景色
    // 设置标题栏跟随窗口变化
    if (this->width() != this->parentWidget()->width())
    {
        this->setFixedWidth(this->parentWidget()->width());
    }
    QWidget::paintEvent(event);
}

void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (maxButton->isVisible())
    {
        onMaxButtonClicked();
    }
    else
    {
        onRestorButtonClicked();
    }
    return QWidget::mouseDoubleClickEvent(event);
}

void TitleBar::mousePressEvent(QMouseEvent *event)
{
    if (maxButton->isVisible())
    {
        isPressed = false;
    }
    else
    {
        isPressed = true;
        startMovePos = event->globalPos();
    }
    return QWidget::mousePressEvent(event);
}

void TitleBar::mouseMoveEvent(QMouseEvent *event)
{
    if (isPressed)
    {
        QPoint movePoint = event->globalPos() - startMovePos;
        QPoint widgetPos = this->parentWidget()->pos();
        startMovePos = event->globalPos();
        this->parentWidget()->move(widgetPos.x() + movePoint.x(), widgetPos.y() + movePoint.y());
    }
    return QWidget::mouseMoveEvent(event);
}

void TitleBar::mouseReleaseEvent(QMouseEvent *event)
{
    isPressed = false;
    return QWidget::mouseReleaseEvent(event);
}

// 设置标题栏图标;
void TitleBar::setTitleIcon(QString filePath)
{
    if (filePath.endsWith(".svg", Qt::CaseInsensitive))
    {
        QSvgRenderer iconRenderer(filePath);
        QPixmap pixmap3(18, 18);
        pixmap3.fill(Qt::transparent);
        QPainter painter3(&pixmap3);
        iconRenderer.render(&painter3);
        titleIcon->setPixmap(pixmap3);
    } else {
        QPixmap icon(filePath);
        titleIcon->setPixmap(icon.scaled(18, 18));
    }
}

void TitleBar::setHeaderInfor(QString info)
{
    textLabel->setText(info);
}
